<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
            <el-form-item label="货品名称" prop="productName">
                <el-input v-model="queryParams.productName" placeholder="请输入货品名称" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>

          <el-form-item
            label="材料类别"
            prop="department"
          >
            <template>
              <el-select v-model="queryParams.materialCateId" clearable placeholder="请选择材料类别" style="width: 100%">
                <el-option v-for="(na, index) in typeList" :key="index" :label="na.name"
                           :value="na.id"></el-option>
              </el-select>
            </template>
          </el-form-item>


            <el-form-item label="货品简称" prop="productForshort" v-if="false">
                <el-input v-model="queryParams.productForshort" placeholder="请输入货品简称" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="货品品牌" prop="productBrand" v-if="false">
                <el-input v-model="queryParams.productBrand" placeholder="请输入货品品牌" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="计量单位" prop="productUnit">
                <el-input v-model="queryParams.productUnit" placeholder="请输入计量单位" clearable size="small"
                    @keyup.enter.native="handleQuery" v-if="false" />
                <el-select clearable v-model="queryParams.productUnit" placeholder="请选择计量单位" style="width: 100%" @keyup.enter.native="handleQuery" >
                    <el-option
                    v-for="(o,i) in [{label: '公斤', value: '公斤'},
                                        {label: '把', value: '把'},
                                        {label: '瓶', value: '瓶'},
                                        {label: '袋', value: '袋'},
                                        {label: '个', value: '个'},
                                        {label: '套', value: '套'},
                                        {label: '件', value: '件'},
                                        {label: '桶', value: '桶'},
                                        {label: '箱', value: '箱'},
                                        {label: '板', value: '板'},
                                        {label: '双', value: '双'},
                                        {label: '台', value: '台'},
                                        {label: '包', value: '包'},
                                        {label: '张', value: '张'},
                                        {label: '份', value: '份'},
                                         {label: '只', value: '只'},
                                         {label: '提', value: '提'},
                                        {label: '块', value: '块'}
                                        ]" :key="i"
                    :label="o.label"
                    :value="o.value"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="参考价格" prop="productPrice" v-if="false">
                <el-input v-model="queryParams.productPrice" placeholder="请输入参考价格" size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="规格" prop="productSpecification" v-if="false">
                <el-input v-model="queryParams.productSpecification" placeholder="请输入规格" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <!-- <el-form-item label="货品图片" prop="productPhoto">
        <el-input
          v-model="queryParams.productPhoto"
          placeholder="请输入货品图片"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
            <!-- <el-form-item
        label="货品简介"
        prop="productIntro"
      >
        <el-input
          v-model="queryParams.productIntro"
          placeholder="请输入货品简介"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['system:product:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['system:product:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['system:product:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['system:product:export']">导出</el-button>
            </el-col>
        </el-row>

        <el-table v-loading="loading" :data="productList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <!-- <el-table-column
        label="主键id"
        align="center"
        prop="id"
      /> -->
            <el-table-column label="材料类别" align="center" prop="materialCateName" />
            <el-table-column label="货品名称" align="center" prop="productName" />
            <el-table-column label="货品简称" align="center" prop="productForshort" />
            <el-table-column label="货品品牌" align="center" prop="productBrand" />
            <el-table-column label="计量单位" align="center" prop="productUnit" />
             <el-table-column label="参考价格" align="center" prop="productPrice" />
            <el-table-column label="规格" align="center" prop="productSpecification" v-if="false" />
            <el-table-column label="货品图片" align="center" prop="productPhoto" v-if="false"/>
            <el-table-column label="货品简介" align="center" prop="productIntro" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['system:product:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['system:product:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <!-- 添加或修改货品对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="材料类别" prop="materialCateId">
                    <!-- <el-input v-model="form.materialCateId" placeholder="请输入材料类别" /> -->
                    <el-select v-model="form.materialCateId" clearable placeholder="请选择材料类别" style="width: 100%">
                        <el-option v-for="(na, index) in typeList" :key="index" :label="na.name"
                            :value="na.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="货品名称" prop="productName">
                    <el-input v-model="form.productName" placeholder="请输入货品名称" />
                </el-form-item>
                <el-form-item label="货品简称" prop="productForshort">
                    <el-input v-model="form.productForshort" placeholder="请输入货品简称" />
                </el-form-item>
                <el-form-item label="货品品牌" prop="productBrand">
                    <el-input v-model="form.productBrand" placeholder="请输入货品品牌" />
                </el-form-item>
                <el-form-item label="计量单位" prop="productUnit">
                    <el-input v-model="form.productUnit" placeholder="请选择计量单位" v-if="false" />
                    <el-select clearable v-model="form.productUnit" placeholder="请选择计量单位" style="width: 100%">
                        <el-option
                        v-for="(o,i) in [{label: '公斤', value: '公斤'},
                         {label: '把', value: '把'},
                                        {label: '瓶', value: '瓶'},
                                        {label: '袋', value: '袋'},
                                        {label: '个', value: '个'},
                                        {label: '套', value: '套'},
                                        {label: '件', value: '件'},
                                        {label: '桶', value: '桶'},
                                        {label: '箱', value: '箱'},
                                        {label: '板', value: '板'},
                                        {label: '双', value: '双'},
                                        {label: '台', value: '台'},
                                        {label: '包', value: '包'},
                                        {label: '张', value: '张'},
                                        {label: '份', value: '份'},
                                         {label: '只', value: '只'},
                                          {label: '提', value: '提'},
                                        {label: '块', value: '块'}
                                        ]"
                        :key="i"
                        :label="o.label"
                        :value="o.value"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="参考价格" prop="productPrice" >
<!--                    <el-input v-model="form.productPrice" placeholder="请输入参考价格" />-->
                  <el-input-number v-model="form.productPrice" :min="0" :precision="2" :step="0.1"  placeholder="请输入参考价格"></el-input-number>

                </el-form-item>
                <el-form-item label="规格" prop="productSpecification" v-if="false">
                    <el-input v-model="form.productSpecification" placeholder="请输入规格" />
                </el-form-item>
                <el-form-item label="货品图片" prop="productPhoto" v-if="false">
                    <el-input v-model="form.productPhoto" placeholder="请输入货品图片" />
                </el-form-item>
                <el-form-item label="货品简介" prop="productIntro">
                    <el-input v-model="form.productIntro" placeholder="请输入货品简介" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    listProduct,
    getProduct,
    delProduct,
    addProduct,
    updateProduct,
    exportProduct,
} from "@/api/productadmin/productadmin";

import { listCate } from '@/api/materialCate/cate';
export default {
    name: "Product",
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 总条数
            total: 0,
            // 货品表格数据
            productList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                productName: undefined,
                productForshort: undefined,
                productBrand: undefined,
                productUnit: undefined,
                productPrice: undefined,
                productSpecification: undefined,
                productPhoto: undefined,
                productIntro: undefined,
                materialCateId: undefined,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
              productPrice: [
                {required: true, message: '请输入参考价格', trigger: 'change'}
              ],
              materialCateId: [
                {required: true, message: '请选择材料类别', trigger: 'change'}
              ],
              productName: [
                {required: true, message: '请输入货品名称', trigger: 'change'}
              ],
              productForshort: [
                {required: true, message: '请输入货品品牌', trigger: 'change'}
              ],
              productUnit: [
                {required: true, message: '请选择计量单位', trigger: 'change'}
              ],
            },
            //   材料类别
            typeList: []
        };
    },
    created() {
        this.getList();
        listCate().then((response) => {
            this.typeList = response.rows;
            this.total = response.total;
            this.loading = false;
        });
    },
    methods: {
        /** 查询货品列表 */
        getList() {
            this.loading = true;
            listProduct(this.queryParams).then((response) => {
                this.productList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: undefined,
                productName: undefined,
                productForshort: undefined,
                productBrand: undefined,
                productUnit: undefined,
                productPrice: undefined,
                productSpecification: undefined,
                productPhoto: undefined,
                productIntro: undefined,
                materialCateId: undefined,
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.queryParams.materialCateId=undefined,
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id);
            this.single = selection.length != 1;
            this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加货品";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids;
            getProduct(id).then((response) => {
                let { materialCateId} = response.data;
                this.form = response.data;
                this.form.materialCateId = materialCateId * 1;
                this.open = true;
                this.title = "修改货品";
            });
        },
        /** 提交按钮 */
        submitForm: function () {
            this.$refs["form"].validate((valid) => {
                if (valid) {
                    let {name} = this.typeList.filter(o => o.id == this.form.materialCateId)[0];
                    this.form.materialCateName = name;
                    if (this.form.id != undefined) {
                        updateProduct(this.form).then((response) => {
                            this.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addProduct(this.form).then((response) => {
                            this.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$confirm('是否确认删除货品编号为"' + ids + '"的数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(function () {
                    return delProduct(ids);
                })
                .then(() => {
                    this.getList();
                    this.msgSuccess("删除成功");
                })
                .catch(function () { });
        },
        /** 导出按钮操作 */
        handleExport() {
            const queryParams = this.queryParams;
            this.$confirm("是否确认导出所有货品数据项?", "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(function () {
                    return exportProduct(queryParams);
                })
                .then((response) => {
                    this.download(response.data);
                })
                .catch(function () { });
        },
    },
};
</script>
